<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" >
            <a-form-item label="院系">
              <a-select :allowClear="true" @change="yxChange" optionFilterProp="children" showSearch v-model="queryParam.yxm">
                <a-select-option :key="i.id" :value="i.dwh" v-for="i in selectMenu.yx">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="所属基地">
                <a-select allowClear v-model="queryParam.jdId" showSearch  :filterOption="filterOption">
                  <a-select-option :key="i.id" :value="i.id" v-for="i in selectMenu.jd">{{ i.lhpyjdmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPESHZT" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="研究方向">
                <a-input v-model="queryParam.yjfx"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="起聘时间">
                <a-range-picker v-model="queryParam.qpsj"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="解聘时间">
                <a-range-picker v-model="queryParam.jpsj"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
            <a-form-item label="导师类别">
              <a-select allowClear v-model="queryParam.dslx">
                <a-select-option v-for="i in LHPYJDDSLX" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button type="primary" v-hasPermission="'jdds:delete'" @click="batchDelete">批量删除</a-button>
              <a-button type="primary" v-hasPermission="'jdds:plsh'" @click="showCheck">批量审核</a-button>
              <a-button type="primary" v-hasPermission="'jdds:tjsh'" @click="shtjFn()">批量提交</a-button>
              <a-button type="primary" @click="exportExcel">导出</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 1600 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="psffrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <span slot="pszzrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <!-- <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a> -->
        <template slot="operation" slot-scope="a,b">
          <!-- 当审核状态为00的时候,才会出现修改按钮 -->
        <template  v-if="b.shzt == '00' || b.shzt == '03' || b.shzt == '01'">
          <a @click="toEdit(b)">修改</a>
          <!-- <a-divider v-hasPermission="'jdds:plsh'" type="vertical" /> -->
        </template>
          <!-- 当有权限,且审核状态不为00的时候出现修改按钮 -->
          <a @click="toEdit(b)" v-hasPermission="'jdds:add'" v-if="b.shzt == '02'">修改</a>


          <template   v-if="b.shzt != '02' && b.shzt != '03' ">
              <a @click="showCheck(b)" v-hasPermission="'jdds:plsh'" >审核</a>

          </template>

          <template  v-if="b.shzt != '02' ">

              <a @click="shtjFn(b)" v-hasPermission="'jdds:tjsh'">提交</a>
          </template>

        </template>
      </s-table>
    </a-card>

    <check-modal
          ref="checkModal"
          :visible.sync="visible"
          @close="() => { visible = false }"
          @save="batchCheck"
        />
  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import checkModal from './component/checkModal'

  export default {
  data () {
    return {
      visible: false,
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '姓名',
          dataIndex: 'xm',
          width:100
        },
        {
          title: '编号',
          dataIndex: 'dsbh',
          scopedSlots: { customRender: 'dsbh' }
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '导师类别',
          dataIndex: 'dslxmc',
          width:160
        },
        {
          title: '所属基地',
          dataIndex: 'jdmc'
        },
        {
          title: '所属单位',
          dataIndex: 'dwmc'
        },
        {
          title: '起聘日期',
          dataIndex: 'qpsj',
          scopedSlots: { customRender: 'psffrq' }
        },
        {
          title: '解聘日期',
          dataIndex: 'jpsj',
          scopedSlots: { customRender: 'pszzrq' }
        },
         {
          title: '研究方向',
          dataIndex: 'yjfx',
        },
         {
          title: '审核状态',
          dataIndex: 'shztmc',
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: '180px',
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        this.$refs.table && this.$refs.table.clearSelected()
        parameter.pageNum = parameter.pageNo
        console.log(this.queryParam)
        let params = Object.assign({},this.queryParam, parameter)
        if(params.qpsj && params.qpsj.length > 0){
            params.qpkssj = params.qpsj[0].format('YYYY-MM-DD');
            params.qpjssj = params.qpsj[1].format('YYYY-MM-DD');
            delete params.qpsj
        }
        if( params.jpsj && params.jpsj.length > 0){
            params.jpkssj = params.jpsj[0].format('YYYY-MM-DD');
            params.jpjssj = params.jpsj[1].format('YYYY-MM-DD');
            delete params.jpsj
        }
        this.$DateToString(params)
        return this.$api.baseTeacher.list(params).then(res => {
          console.log(res.data)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {},
      // recruitVisible: false,
      // breakVisible: false,
      teacherInfo: {},
      initDone: false,
      checkId:"" //单个审核选中ID
    }
  },
  components: {STable, checkModal},
  computed: {
    ...mapState({
      LHPYJDDSLX: state => state.app['LHPYJDDSLX'], // 导师类别
      TYPESHZT: state => state.app['TYPESHZT'], // 导师状态
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      // TYPE0387: state => state.app['TYPE0387'] // 招生学年
    })
  },
  beforeRouteEnter (to, from, next) {
    console.log(from.name)
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if ((from.name === '基地导师管理' || from.name === '基地导师信息修改' ) && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      vm.initDone = true
    })
  },
  beforeRouteLeave (to, from, next) {
    console.log(to.name)
    if (to.name === '基地导师管理' || to.name === '基地导师信息修改' ) {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'LHPYJDDSLX')
    this.$store.dispatch('app/setDictionary', 'TYPESHZT')
    this.$store.dispatch('app/setDictionary', 'TYPE0321')
    // this.$store.dispatch('app/setDictionary', 'TYPE0387')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),

      ]

      Promise.all(allRequest).then(res => {
        console.log("allRequest",res)
        this.selectMenu = {
          yx: res[0].data,
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    showCheck (data) {
      if(data.id){
        this.checkId = data.id
        this.visible = true
      }else{
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要审核的导师')
          return
        }
        var flag =false
        this.selectedRows.forEach(item => {
          if(item.shzt != '01'){
            flag = true
            return
          }
        })
        if(flag){
           this.$message.warning('不能选择已审核的数据')
            return
        }
        this.checkId = ""
        this.visible = true
      }
    },
    yxChange (code) {
      this.$set(this.queryParam, 'jdId', '')
      if (code) {
        this.$api.base.unionBaseALlForTeacher({yxm:code}).then(res => {
          this.$set(this.selectMenu, 'jd', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'jd', [])
      }
    },
    batchCheck (params) {
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = []
      if(this.checkId){
        //单个审核
          dsbhs = [this.checkId]
      }else{
        //多选审核
         dsbhs = this.selectedRows.map(ds => {
          return ds.id
        })
      }
      this.$api.baseTeacher.teacherCheck({ids: dsbhs, ...params}).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.visible = false
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
        this.$refs.checkModal.form.resetFields()
      }).finally(() => {
        this.$refs.checkModal.confirmLoading = false
      })
    },
    shtjFn(data){
      if (!this.selectedRowKeys.length && !data) {
          this.$message.warning('请选择需要提交审核的导师')
          return
      }
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = []
      if(data && data.id){
        //单个提交
          dsbhs = [data.id]
      }else{
        // console.log(this.selectedRows.includes({'shzt':'02'}))
        // if(this.selectedRows.include({shzt:'02'}))
        var flag = true
        this.selectedRows.map(item => {
          if(item.shzt == '02'){
            flag = false
            return false
          }
        })
        if(!flag){
          this.$message.warning('不能提交已审核的数据')
          return
        }

        //多选提交
         dsbhs = this.selectedRows.map(ds => {
          return ds.id
        })
      }
      this.$api.baseTeacher.teacherShtj(Object.assign({ids: dsbhs})).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
      }).finally(() => {
        this.$refs.checkModal.confirmLoading = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // direct (data) {
    //   this.$router.push(`/teacher/info/${data.dsbh}`)
    // },
    toEdit (data) {
      this.$router.push(`/degree/trainingBase/teacher/${data.id}/edit`)
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的导师')
        return
      }
      this.$confirm({
        title: '确定删除所选中的导师信息?',
        content: '当您点击确定按钮后，这些导师信息将会被彻底删除',
        centered: true,
        onOk: () => {
          let teacherIds = []
          this.selectedRows.forEach(teacher => {
            teacherIds.push(teacher.id)
          })
          this.$api.baseTeacher.delete(teacherIds.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    handleChange (info) {
      console.log(info)
    },
    exportExcel () {
      let params = this.queryParam
      this.$export('/degree/jdds/export', {
          ...params
      })
    }
  }
}
</script>

<style>

</style>
